<!--
 * @Author: wangzm
 * @Date: 2020-01-19 10:45:00
 * @LastEditors: wjk
 * @LastEditTime: 2020-04-21 13:31:25
 * @Description: guide-container
 -->
<template>
  <base-page-view class-name="guide-container">
    <b-image class-name="img" :src="launch.pic" mode="aspectFill" @load="imgLoad" @error="imgLoad" @click="launchClick" />
    <view class="time-counter" @click="toHomePage">跳过 {{ time }}</view>
  </base-page-view>
</template>

<script>
import './index.scss'
export default {
  components: {
  },
  data() {
    return {
      launch: {
        pic: ''
      },
      time: 3,
      timer: null
    }
  },
  created() {
  },
  onLoad() {
    this.time = 3
    this.timer = null
    this.fetchData()
  },
  methods: {
    async fetchData() {
      const res = await this.$api.commonApi.queryLaunch()
      if (res.success && res.data) {
        this.launch = res.data.launch
        uni.setStorage({
          key: 'home_pop',
          data: res.data.home_pop
        })
      } else {
        this.toHomePage()
      }
    },
    imgLoad() {
      this.timer = setInterval(() => {
        this.time--
        if (this.time === 0) {
          this.toHomePage()
        }
      }, 1000)
    },
    toHomePage() {
      clearInterval(this.timer)
      uni.setStorage({
        key: 'guideShowFlag',
        data: true
      })
      this.$navigateTo(`/pages/home/index`, {}, 'reLaunch')
    },
    launchClick() {
      if (this.launch.type_val) {
        uni.setStorage({
          key: 'launch_data',
          data: this.launch
        })
        this.$sensorTrack('OPEN_ADVERT', {
          button_name: 'advert',
          page_name: 'main'
        })
        this.toHomePage()
      }
    },
    getLocation() {
      uni.getLocation({
        type: 'wgs84',
        success: async(data) => {
          uni.setStorage({
            key: 'BX_Location',
            data
          })
        }
      })
    }
  }
}
</script>
